<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer extends CI_Controller {

	public function index(){
	   $this->load->model('Customers', null, true);
       $data['Customers'] = $this->Customers->getList();
	   $this->load->view('customer/index', $data);
	}
    
    /**
     * function add customer
     * @author tien.nguyen
     */
    public function add(){
        $data = array();
        $this->load->model('Customers', null, true);
        $this->load->model('Videos', null, true);
        $this->load->model('CustomerVideos', null, true);
        
        //get post
        if($this->input->post()){
            $data = $this->input->post();
            $customerId = $this->Customers->add($data);
            if($customerId){
                //insert customer video
                if($data['Videos']){
                    foreach($data['Videos'] as $item){
                        $dataVideo['CustomerId'] = $customerId;
                        $dataVideo['VideoId'] = $item;
                        $this->CustomerVideos->add($dataVideo);
                    }
                }
                redirect(base_url().'customer.html');
            }
            $data['Customer'] = $data;
        }
        $data['OptionsVideos'] = $this->Videos->getOptions();
        $data['CustomerVideos'] = array();
        $this->load->view('customer/add', $data);
    }
    
    /**
     * Delete customer
     * @author tien.nguyen
     */
    public function delete($customerId){
         $this->load->model('Customers', null, true);
         if($this->Customers->delete($customerId)){
            redirect(base_url().'customer.html');
         }
    }
    
    /**
     * edit save config id
     * @author tien.nguyen
     */
    public function edit($customerId){
        $res = false;
        $this->load->model('Customers', null, true);
        $this->load->model('Videos', null, true);
        $this->load->model('CustomerVideos', null, true);
        
        if($this->input->post()){
            $data = $this->input->post();          
            if($this->Customers->edit($data, $customerId)){
                //insert customer video
                if($data['Videos']){
                    $this->CustomerVideos->delete($customerId);
                    foreach($data['Videos'] as $item){
                        $dataVideo['CustomerId'] = $customerId;
                        $dataVideo['VideoId'] = $item;
                        $this->CustomerVideos->add($dataVideo);
                    }
                }
                redirect(base_url().'customer.html');          
            }    
        }
        
        $data['Customer'] = $this->Customers->getCustomer($customerId);
        $data['OptionsVideos'] = $this->Videos->getOptions();
        $data['CustomerVideos'] = $this->CustomerVideos->getCustomerVideo($customerId);
        $this->load->view('customer/edit', $data);       
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */